package com.scrumtrek.trainings.tdd;

import com.google.common.collect.Lists;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: zeliboba
 * Date: 11.09.13
 * Time: 10:12
 * To change this template use File | Settings | File Templates.
 */
public class BookStoreService{

    private IStore<Book> store;

    public void setStore(IStore<Book> store) {
        this.store = store;
    }

    public void addBook(Book book) {
        store.addItem(book);
    }

    public Set<String> getAuthors() {
        Set<String> authors = new HashSet<String>();

        for (Book b : store.getItemList()) {
            authors.add(b.getAuthor());
        }
        return authors.isEmpty() ? null : authors;
    }

    public Set<String> getTitles() {
        Set<String> titles = new HashSet<String>();

        for (Book b : store.getItemList()) {
            titles.add(b.getTitle());
        }
        return titles;
    }

    public List<Book> findByAuthor(String author) {

        List<Book> list = Lists.newArrayList();
        if (author==null) {
            return list;
        }
        for (Book b : store.getItemList()) {
            if(author.equals(b.getAuthor())) {
                list.add(b);
            }
        }
        return list;
    }

    public Book findByTitle(String title) {
        for (Book b : store.getItemList()) {
            if(b.getTitle().equals(title)) {
                return b;
            }
        }
        return null;
    }
    public void sell(Book item){
        store.sell(item);
    }

    public int getTotalBookAmount() {
        return store.getTotalItemsAmount();
    }
}
